'''
https://leetcode.cn/problems/find-k-length-substrings-with-no-repeated-characters
'''
from collections import Counter


class Solution:
    def numKLenSubstrNoRepeats(self, s: str, k: int) -> int:
        cnt = Counter()
        kind = 0
        for i in range(k - 1):
            cnt[s[i]] += 1
            if cnt[s[i]] == 1:
                kind += 1

        l, r = 0, k - 1
        res = 0
        while r < len(s):
            cnt[s[r]] += 1
            if cnt[s[r]] == 1:
                kind += 1
            if kind == k:
                res += 1
            cnt[s[l]] -= 1
            if cnt[s[l]] == 0:
                kind -= 1
            l, r = l + 1, r + 1
        return res

s = 'havefunonleetcode'
k = 5
print(Solution().numKLenSubstrNoRepeats(s, k))